package com.itheima.a10threadsafe2;

public class MyRunnable implements Runnable{


    /**
     * 循环
     * 同步代码块
     * 判断共享数据是否到末尾
     * 判断共享数据是否没到末尾
     */
    int ticket = 0 ;

    // ctrl + alt + m 将选中代码抽象成方法

    private synchronized boolean method () {

            if ( ticket == 100 ) {
                return true;    //退出循环
            } else {
                ticket ++ ;
                System.out.println(Thread.currentThread().getName() + "正在卖第：" + ticket + "张票");
            }

            return false ;
        }

    /**
     * Runs this operation.
     */
    @Override
    public void run() {
        while ( true ) {
            if (method()) {     //同步方法判断循环退出条件
                break;
            }
    }
}
}
